package com.mikey.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mikey.common.PageResult;
import com.mikey.mapper.TbBrandMapper;
import com.mikey.pojo.TbBrand;
import com.mikey.pojo.TbBrandExample;
import com.mikey.service.BrandService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @author :Mikey
 * @description :
 * @create :2022-06-28 10:52:00
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private TbBrandMapper tbBrandMapper;

    public List<TbBrand> findAll() {
        return tbBrandMapper.selectByExample(null);
    }

    public TbBrand findOne(Long id) {
        return tbBrandMapper.selectByPrimaryKey(id);
    }

    public int deleteBrand(Long id) {
        return tbBrandMapper.deleteByPrimaryKey(id);
    }

    public int updateBrand(TbBrand tbBrand) {
        return tbBrandMapper.updateByPrimaryKeySelective(tbBrand);
    }

    public int addBrand(TbBrand tbBrand) {
        return tbBrandMapper.insertSelective(tbBrand);
    }

    public int deleteByIds(Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            tbBrandMapper.deleteByPrimaryKey(ids[i]);
        }
        return 1;
    }

    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<TbBrand> page = (Page) tbBrandMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    public PageResult findByKwd(TbBrand tbBrand, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        TbBrandExample example= new TbBrandExample();
        TbBrandExample.Criteria criteria=example.createCriteria();
        if (tbBrand.getName()!=null&&tbBrand.getName().length()>0){
            criteria.andNameLike("%"+tbBrand.getName()+"%");
        }
        if (tbBrand.getFirstChar()!=null&&tbBrand.getFirstChar().length()>0){
            criteria.andFirstCharEqualTo(tbBrand.getFirstChar());
        }
        Page<TbBrand> page = (Page) tbBrandMapper.selectByExample(example);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Map> selectOptionList() {
        return tbBrandMapper.selectOptionList();
    }


}
